package main

import (
	"fmt"
	"math"
)

type TreeNode struct {
	Val         int
	Left, Right *TreeNode
}

func maxDepth(root *TreeNode) int {
	if root == nil {
		return 0
	}
	leftH := maxDepth(root.Left) + 1
	rightH := maxDepth(root.Right) + 1
	if leftH > rightH {
		return leftH
	} else {
		return rightH
	}
}

func isBalanced(root *TreeNode) bool {
	if root == nil {
		return true
	}
	leftH := maxDepth(root.Left)
	rightH := maxDepth(root.Right)
	h := math.Abs(float64(leftH) - float64(rightH))
	if h <= 1 {
		return isBalanced(root.Right) && isBalanced(root.Left)
	} else {
		return false
	}
}

func main() {
	var a, b TreeNode
	a.Right = &b
	fmt.Println(a, b)
	fmt.Println(isBalanced(&a))
}
